package dyy.zc.computergradeexam.ui.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dyy.zc.computergradeexam.R;
import dyy.zc.computergradeexam.base.BaseActivity;
import dyy.zc.computergradeexam.bean.Knowledge;
import dyy.zc.computergradeexam.utils.BmobUtils;
import dyy.zc.computergradeexam.utils.ToastUtils;

public class KnowledgeActivity extends BaseActivity implements View.OnClickListener {
    private TextView content,title;
    private BmobUtils bmobUtils;
    private List<Knowledge> list = new ArrayList<>();
    private int count = 0;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1://获取知识点
                    Collections.sort(list);
                    setKnowledge();
                    break;
            }
        }
    };

    private void setKnowledge(){
        title.setText(list.get(count).getTitle());
        content.setText(list.get(count).getContent());
    }

    @Override
    protected void findViewByIDS() {
        content = findViewsById(R.id.content);
        title = findViewsById(R.id.top_tv_title);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledge);
        initData();
        bind();
    }

    private void bind() {
        findViewsById(R.id.top_tv_next).setOnClickListener(this);
        findViewsById(R.id.top_tv_pre).setOnClickListener(this);
    }

    private void initData() {
        bmobUtils = new BmobUtils(this);
        bmobUtils.getKnowledge(list,1,handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_tv_next:
                count++;
                if (list.size()>count){
                    setKnowledge();
                }else {
                    count = list.size()-1;
                    ToastUtils.shortToast(KnowledgeActivity.this,"没有下一章了~~");
                }
                break;
            case R.id.top_tv_pre:
                count--;
                if (count > -1){
                    setKnowledge();
                }else {
                    count = 0;
                    ToastUtils.shortToast(KnowledgeActivity.this,"没有上一章了~~");
                }
                break;
        }
    }
}
